#pragma strict
@script AddComponentMenu("Level4/Cameras/GatlingCamera")
///////////////////////////////////////////////////////////
//固定角度摄影机,主体
//思路:用变量offsetPos存放,摄影机与target的位置关系,
//修改offsetPos可以对摄影机的位置实现不同的控制.
///////////////////////////////////////////////////////////
/** 摄影机的目标 */
var target:Transform;
/** 与目标位置的偏移向量 */
var offsetPos:Vector3 = Vector3(0,11,-7);
/** 跟随target的速度 */
var followSpeed:float = 0.02;
/** 当前player是否在跑步 */
var isRunning:boolean;
/** 是否跟踪 */
var isFollow:boolean = true;
//fov范围[最小,最大]
var fovRange:Vector2 = Vector2(60,65);
//fov恢复因数
var fovResumeFactor:float = 0.1;
private var tr:Transform;
private var cam:Camera;

function Awake(){
	tr = transform;
	cam = camera;
	fovRange.x = cam.fieldOfView;
}

var isShake:boolean;
function Update(){
	if(isShake){
		isShake = false;
		Shake();
	}
	Resume();
}

function Shake(){
	cam.fieldOfView = fovRange.y;
}

function Resume(){
	if(cam.fieldOfView > fovRange.x){
		cam.fieldOfView = Mathf.Lerp(cam.fieldOfView,fovRange.x,fovResumeFactor);
	}
}

/**
设定 摄影机的旋转量.
*/
function SetupCamera(){
	tr.rotation = Quaternion.LookRotation(-offsetPos);
}
//更新 摄影机的坐标.
function UpdatePosition(h:boolean,v:boolean,info:GatlingRangeInfo){
	if(isFollow && target){
		var isUpdatePos:boolean = (info.cameraPos != Vector3.zero);
		var offset:Vector3 = isUpdatePos ? info.cameraPos : offsetPos;
		var targetPos:Vector3 = target.position + offset;
		if(!h)
			targetPos.x = tr.position.x;
		if(!v)
			targetPos.z = tr.position.z;
		if(!isUpdatePos)
			targetPos.y = offsetPos.y;
		// update
		tr.position = Vector3.Slerp(tr.position,targetPos,isRunning ? followSpeed * 2 : followSpeed);
		if(info.cameraRotation != Vector3.zero)
			tr.rotation = Quaternion.Slerp(tr.rotation,Quaternion.Euler(info.cameraRotation),info.rotateSpeed);
	}
}
